package com.hr.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.hr.dao.ManagerDao;
import com.hr.model.ManagerModel;
@Repository
public class ManagerDaoImpl implements ManagerDao{
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Override
	public List<ManagerModel> find(String account,String passwrod) {
		String sqlStr = "select id,account,password,managerLevel,validInd from tb_manager where account=? ";
		List<ManagerModel> result = jdbcTemplate.query(sqlStr, ParameterizedBeanPropertyRowMapper.newInstance(ManagerModel.class),account);
		return result;
	}
	
	@Override
	public ManagerModel findById(String id) {
		String sql = "select * from tb_manager where id = ?";
		return jdbcTemplate.queryForObject(sql, ParameterizedBeanPropertyRowMapper.newInstance(ManagerModel.class), id);
	}
	
	@Override
	public List<ManagerModel> findAll() {
		String sql = "select * from tb_manager";
		List<ManagerModel> list = jdbcTemplate.query(sql, ParameterizedBeanPropertyRowMapper.newInstance(ManagerModel.class));
		return list;
	}
	
	@Override
	public void deleteById(String id) {
		String sql = "delete from tb_manager where id = ?";
		jdbcTemplate.update(sql, id);
		
	}
	@Override
	public ManagerModel updateById(ManagerModel managerModel) {
		String sql = "update tb_manager set id=:id,account=:account,password=:password,managerLevel=:managerLevel,validInd=:validInd where id=:id";
		SqlParameterSource sqlParameterSource = new BeanPropertySqlParameterSource(managerModel);
		jdbcTemplate.update(sql, sqlParameterSource);
		return managerModel;
	}

	@Override
	public void excuteBySql(String sql) {
		jdbcTemplate.execute(sql);
	}

}
